﻿
@{
    ViewBag.Title = "GetDraw";
    Layout = "~/Views/Shared/_MyLayoutPage.cshtml";
}

@section head{
    <link rel="stylesheet" href="~/Content/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css" />
    <link rel="stylesheet" href="~/Content/zTree_v3/css/demo.css" type="text/css" />
    <script type="text/javascript" src="~/Content/zTree_v3/js/jquery.ztree.all.js"></script>

    <style>
        html, body {
            height: 100%;
            width: 100%;
        }

        .parent {
            display: flex;
            height: 100%;
            width: 100%;
        }

        .left {
            width: 400px;
            height: 100%;
            background-color: white;
        }

        .right {
            display: flex;
            flex: 1;
            height: 100%;
            background-color: whitesmoke;
        }

        .ztree li span.button.add {
            margin-left: 2px;
            margin-right: -1px;
            background-position: -144px 0;
            vertical-align: top;
            *vertical-align: middle
        }
    </style>
}

<div class="parent">
    <div class="left">
        <div>
            <ul id="treeDemo" class="ztree" style="width:100%;height:100%;"></ul>
        </div>
    </div>
    <div class="right">
        <div>
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>图纸领取</legend>
            </fieldset>
            <div>
                <ul style="list-style:none;list-style-type: none;">
                    <li style="font-size:20px;list-style-type:none;">选中的节点名称：<span id="SelNodeName"></span></li>
                    <li style="font-size:20px;list-style-type:none;">选中的节点编号：<span id="SelNodeNum"></span></li>
                    <li style="font-size:20px;list-style-type:none;">图纸序列编号：<span id="SelSerialNum"></span></li>
                </ul>
            </div>

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>图号领取明细</legend>
            </fieldset>
            <form class="layui-form" enctype="multipart/form-data">
                <div class="layui-form-item">
                    <label class="layui-form-label">搜索</label>
                    <div class="layui-input-block">
                        <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入图纸号" class="layui-input" style="width:300px;float:left;">
                        <input type="text" value="" style="display:none" />
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-inline">
                            <select name="islinkman" id="DrawTypeSearchSel" required lay-verify="required">
                                <option value="0">全部</option>
                                <option value="零部件">零部件</option>
                                <option value="组件">组件</option>
                                <option value="资料">资料</option>
                            </select>
                        </div>
                        <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                        <button type="button" class="layui-btn layui-btn-normal" style="float:left;" id="BtnGetDraw"><i class="layui-icon">&#xe621;</i>领取图号</button>

                    </div>
                </div>
            </form>

            <table id="demo" lay-filter="UserTableList"></table>
        </div>
    </div>
</div>



<div id="addLayer" style="display:none;">
    <form class="layui-form" id="AddFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            图纸序列名称：<span id="AddLayerDrawName"></span>
        </div>
        <div class="layui-form-item">
            图纸序列编号：<span id="AddLayerDrawNum"></span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图纸类型</label>
            <div class="layui-input-inline">
                <select name="islinkman" id="DrawTypeSel" required lay-verify="required">
                    <option value="零部件">零部件</option>
                    <option value="组件">组件</option>
                    <option value="资料">资料</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电机型号</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="MotorId" style="width:300px;" lay-verify="required" placeholder="请输入电机型号" class="layui-input">
            </div>
        </div>
        <div>
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="memo" style="width:300px;" lay-verify="required" placeholder="请输入备注" class="layui-input">
            </div>
        </div>

    </form>
</div>



<SCRIPT type="text/javascript">

    var setting = {
        view: {
            expandSpeed: "",
            selectedMulti: false
        },
        edit: {
            enable: false,
            showRemoveBtn: false,
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick,
        }
    };
    var newCount = 1;
    var TreeNodeId = 0;

    function zTreeOnClick(event, treeId, treeNode) {

        var data = {};
        data["Id"] = treeNode.Id;
        TreeNodeId = treeNode.Id;

        $.ajax({
            type: "POST",
            url: "/Api/GetAllTreeByNode",
            async: false,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (msg) {
                if (msg.result == "OK") {
                    $("#SelNodeName").text(msg.NodeNameSerial);
                    $("#SelNodeNum").text(treeNode.DrawNum);
                    $("#SelSerialNum").text(msg.DrawNumSerial);

                } else {
                    alert(msg.result);
                    return;
                }
            }
        });

    };


    var TreeJson = {};
    var TreeJsonStr = '@Html.Raw(ViewBag.TreeJsonStr)';
    TreeJson = JSON.parse(TreeJsonStr);

    $(document).ready(function () {
        //$.fn.zTree.init($("#treeDemo"), setting, zNodes);
        $.fn.zTree.init($("#treeDemo"), setting, TreeJson);
    });
        //-->
</SCRIPT>




<script type="text/javascript">


    var layer;
    var index;
    var table;
    layui.use(['form', 'table', 'upload', 'layer'], function () {
        var form = layui.form;
        var tab = parent.tab;
        var form = layui.form;
        table = layui.table;
        layer = layui.layer;
        
        //新增用户保存
        $("#BtnGetDraw").on("click", function () {
            var tempName = $("#SelNodeName").text();
            var tempNum = $("#SelSerialNum").text();
            $("#AddLayerDrawName").text(tempName);
            $("#AddLayerDrawNum").text(tempNum);
            
            index = layer.open({
                type: 1,
                title: ['新增', 'font-size:18px;'],
                area: '600px',
                content: $("#addLayer")
                , btn: ['确认', '取消']
                , yes: function (index, layero) {
                    //alert("确定");
                    data = {};
                    data["Id"] = TreeNodeId;
                    data["Type"] = $("#DrawTypeSel").val();
                    var MotorId = $("#MotorId").val();
                    if (MotorId == "") {
                        alert("请输入电机型号");
                        return;
                    }
                    var Memo = $("#memo").val();
                    data["Memo"] = Memo;
                    data["MotorId"] = MotorId;
                    $.ajax({
                        type: "POST",
                        url: "/Api/Api_GetDrawNumIncrease",
                        async: false,
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: function (msg) {
                            if (msg.result == "OK") {
                                layer.msg('领取成功', { icon: 6, time: 1000 });
                                table.reload("demo", {
                                    //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                });

                            } else {
                                alert(msg.result);
                                return;
                            }
                        }
                    });
                    layer.close(index);
                    return;
                }
                , btn2: function (index, layero) {
                    return true;
                }
                , cancel: function () {
                    return;
                }
            });
            
        });


        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/api/GetDrawTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "a.id", Order: "desc", Condition: "and a.IsDelete=0" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                { field: 'Type', title: '类型', width: 100 },
                { field: 'SerialDrawName', title: '序列名称', width: 200 },
                { field: 'MotorId', title: '电机型号', width: 200 },
                { field: 'SerialDrawNum', title: '序列编号', width: 120 },
                { field: 'IncreaseNum', title: '自增编号', width: 120 },
                { field: 'UserName', title: '获取用户', width: 100 },
                { field: 'GetTime', title: '获取时间', width: 180 },
                , {
                    field: "operation", title: "操作", width: 150, fixed: "right",
                    templet: function (d) {
                        return '' +
                            '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        table.on("tool(UserTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除该记录？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                    , btn1: function (index) {

                        DataObj = {};
                        DataObj["Id"] = data.Id;

                        $.ajax({
                            type: "POST",
                            url: "/Api/GetDrawNumDel",
                            async: false,
                            contentType: "application/json",
                            dataType: "json",
                            data: JSON.stringify(DataObj),
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });
       


    });


    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        var Type = $("#DrawTypeSearchSel").val();
        if (Type==0) {
            if (text == "") {
                table.reload("demo", {
                    where: {
                        SortName: "a.id", Order: "Desc", Condition: " and a.IsDelete<>1"
                    },
                    page: {
                        curr: 1
                    }
                });
            }
            else {
                table.reload("demo", {
                    where: {
                        SortName: "a.id", Order: "Desc", Condition: " and a.IsDelete<>1 and (SerialDrawNum like '%" + text + "%' or MotorId like '%" + text + "%')"
                    },
                    page: {
                        curr: 1
                    }
                });
            }
        }
        else {
            if (text == "") {
                table.reload("demo", {
                    where: {
                        SortName: "a.id", Order: "Desc", Condition: " and a.IsDelete<>1 and Type='" + Type + "'"
                    },
                    page: {
                        curr: 1
                    }
                });
            }
            else {
                table.reload("demo", {
                    where: {
                        SortName: "a.id", Order: "Desc", Condition: " and a.IsDelete<>1 and (SerialDrawNum like '%" + text + "%' or MotorId like '%" + text + "%') and Type='" + Type + "'"
                    },
                    page: {
                        curr: 1
                    }
                });
            }                
        }

    }

</script>
